//package com.gelian.demo.utils;
//
//import android.content.Context;
//import android.os.Handler;
//import android.os.Message;
//import android.util.Log;
//
//import com.gelian.demo.AppConstant;
//import com.qiniu.android.http.ResponseInfo;
//import com.qiniu.android.storage.Configuration;
//import com.qiniu.android.storage.Recorder;
//import com.qiniu.android.storage.UpCancellationSignal;
//import com.qiniu.android.storage.UpCompletionHandler;
//import com.qiniu.android.storage.UpProgressHandler;
//import com.qiniu.android.storage.UploadManager;
//import com.qiniu.android.storage.UploadOptions;
//import com.qiniu.android.storage.persistent.FileRecorder;
//
//import org.json.JSONException;
//import org.json.JSONObject;
//
//import java.io.File;
//import java.io.FileOutputStream;
//import java.io.IOException;
//import java.io.InputStream;
//import java.io.OutputStream;
//import java.net.HttpURLConnection;
//import java.net.URL;
//
//public class UploadUtil {
//    public static final String TAG = UploadUtil.class.getSimpleName();
//    public static Context context;
//    public static final String SPLIT_STR = "#";
//    public static final int MSG_MAIN_THREAD = 999;
//    private static int lastPer;
//    private static boolean isCancelled;
//
//
//
//    public interface UpLoadListener{
//        void uploadProgress(int percent);
//        void uploadSuccess(String key);
//        void uploadFail(String error);
//        void uploadCancel();
//    }
//
//
//    private static UpLoadListener listener;
////    private static Recorder recorder;
//
////    static {
////        try {//上传文件的进度记录在该路径
////            recorder = new FileRecorder(context.getCacheDir().getAbsolutePath() );
////        } catch (IOException e) {
////            e.printStackTrace();
////        }
////    }
//
//    private static Configuration config = new Configuration.Builder()
//            .chunkSize(256 * 1024)  //分片上传时，每片的大小。 默认 256K
//            .putThreshhold(512 * 1024)  // 启用分片上传阀值。默认 512K
//            .connectTimeout(10) // 链接超时。默认 10秒
//            .responseTimeout(60) // 服务器响应超时。默认 60秒
////                        .recorder(recorder)  // recorder 分片上传时，已上传片记录器。默认 null
////            .recorder(recorder, keyGen)  // keyGen 分片上传时，生成标识符，用于片记录器区分是那个文件的上传记录
//            //            .zone(Zone.zone0) // 设置区域，指定不同区域的上传域名、备用域名、备用IP。默认 Zone.zone0
//            .build();
//
//    // 重用 uploadManager。一般地，只需要创建一个 uploadManager 对象
//    private static UploadManager uploadManager = new UploadManager(config);
//
//    /**
//     * 使用七牛上传文件
//     * @param token      从云端获取
//     * @param file       文件对象
//     * @param listener
//     */
//    public static void uploadFile(String token, File file, final UpLoadListener listener) {
//
//        UploadUtil.listener = listener;
//        isCancelled = false;//该值为true的时候停止上传文件
//        lastPer = 0;        //将文件上传进度重置为0
//        String key = null;//用户不指定key的值,将自动使用文件的hash值作为key。
//
//        uploadManager.put(file
//                , key
//                , token
//                , new UpCompletionHandler() {
//                    @Override
//                    public void complete(String key, ResponseInfo info, JSONObject response) {
//                        Log.d(TAG, "info = " + info);
//                        Log.d(TAG, "response = " + response);
//                        switch (info.statusCode) {
//                            case 200://上传成功时得到该状态
//                                try {
//                                    key = response.getString("key");
//                                } catch (JSONException e) {
//                                    e.printStackTrace();
//                                }
//                                if (listener!= null){
//                                    listener.uploadSuccess(key);
//                                }
//                                break;
//                            case -2://当取消上传时得到该状态
//                                if (listener!= null){
//                                    listener.uploadCancel();
//                                }
//                                break;
//                            default:
//                                if (listener!= null){
//                                    listener.uploadFail(info.error);
//                                }
//                                break;
//                        }
//                    }
//                }
//                , uploadOptions);
//    }
//
//    /**
//     * 停止文件上传
//     */
//    public static void stopUpload(){
//        isCancelled = true;
//    }
//
//    private static UploadOptions uploadOptions = new UploadOptions(null
//            , null
//            , false
//            , new UpProgressHandler() {
//        @Override
//        public void progress(String key, double percent) {
//            //文件上传的进度
//            int percentInt = (int) (percent * 100);
//            if (percentInt > lastPer) {
//                lastPer = percentInt;
//                if (listener!= null){
//                    listener.uploadProgress(percentInt);
//                }
//            }
//        }
//    }
//            , new UpCancellationSignal() {
//        @Override
//        public boolean isCancelled() {
//            return isCancelled;
//        }
//    });
//
//
//    public static void download(final String filename, final String urlStr) {
//
//        ThreadUtil.getSinglePool().execute(new Runnable() {
//            @Override
//            public void run() {
//                String path = AppConstant.PATH_DIR + filename;
//                OutputStream output = null;
//                InputStream input;
//                try {
//                     /*
//                     * 通过URLf取得HttpURLConnection
//                     * 要网络连接成功，需在AndroidMainfest.xml中进行权限配置
//                     * <uses-permission android:name="android.permission.INTERNET" />
//                     */
//                    URL url = new URL(urlStr);
//                    Log.d(TAG, "开始下载: " + urlStr);
//                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//                    input = conn.getInputStream();
//                    output = new FileOutputStream(path);
//                    //读取大文件
//                    byte[] buffer = new byte[4 * 1024];
//                    int len;
//                    while ((len = input.read(buffer)) != -1) {
//                        output.write(buffer,0,len);
//                    }
//                    output.flush();
//
//                    Log.d(TAG, "下载完成: " + path);
//                    Message msg = handler.obtainMessage();
//                    msg.what = MSG_MAIN_THREAD;
//                    msg.obj = filename;
//                    handler.sendMessage(msg);
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }finally {
//                    if (output != null){
//                        try {
//                            output.close();
//                        } catch (IOException e) {
//                            e.printStackTrace();
//                        }
//                    }
//                }
//            }
//        });
//
//    }
//
//
////    public static Map<String, Boolean> MAP_FILENAME_UPLOADING = new HashMap<>();
//
////    public static void requestPercent(final String imei, final String filename, String taskid) {
////        Message msg = handler.obtainMessage();
////        msg.what = (String) id;
////
////        msg.obj = filename + SPLIT_STR + taskid;
////        handler.sendMessageDelayed(msg, 3000);
////
////
////        GlEngine.queryFileUploadProgress(imei, filename, taskid, new QueryUploadProgresCallback() {
////            @Override
////            public void done(int resultCode, String percent) {
////
////                switch (resultCode){
////                    case   GlResultCode.GL_CALLBACK_CODE_SUCCESS:
////                        try {
////                            int per = Integer.parseInt(percent);
////                            if (per < 0) {
////                                UploadUtil.MAP_FILENAME_UPLOADING.remove(filename);
////                                return;
////                            }
////                        }catch (Exception e){
////                            UploadUtil.MAP_FILENAME_UPLOADING.remove(filename);
////                            return;
////                        }
////                        Log.d(TAG, "percent = " + percent + "%");
////                        Toastor.showToast("设备" + imei + "分享的文件:" + filename
////                                + "已经上传完成" + percent + "%"
////                        );
////                        break;
////                    case GlResultCode.GL_DEVICE_STATUS_TARGET_OFFLINE:
////                        UploadUtil.MAP_FILENAME_UPLOADING.remove(filename);
////                        break;
////
////                }
////            }
////        });
////    }
////
//
//    public static Handler handler = new Handler() {
//        @Override
//        public void handleMessage(Message msg) {
//            if ( msg.what == MSG_MAIN_THREAD) {
//                String filename = (String) msg.obj;
//                Toastor.showLongToast("文件:" + filename
//                        + "已经下载完成,存放于手机上的gelian文件夹"
//                );
////                return;
//            }
//
////            String[] split = ((String) msg.obj).split(SPLIT_STR);
////            String filename = split[0];
////            String taskid = split[1];
////            Boolean uploading = MAP_FILENAME_UPLOADING.get(filename);
////            if (uploading != null
////                    && uploading
////                    ) {
////                UploadUtil.requestPercent(imei, filename, taskid);
////            }
//        }
//    };
//
//
//}
